package org.example.multiThreading.threadArray;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestCopyOnWriteArrayList {
    public static void main(String[] args) {
        CopyOnWriteArrayList<Object> list = new CopyOnWriteArrayList<>();
        //创建线程池
        ExecutorService service = Executors.newFixedThreadPool(5);

        for (int i = 0; i < 2; i++) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    for (int i1 = 0; i1 < 1000; i1++) {
                        list.add(i1);
                    }
                }
            };
            service.execute(runnable);
        }

        //关闭线程池
        service.shutdown();

        //查询线程是否全部执行完毕
        while (!service.isTerminated()){}

        System.out.println(list.size());
    }
}
